#version 400 core

#define POSITION	0
#define NORMAL		1
#define COLOR		2
#define TEXCOORD	3
#define FRAG_COLOR	0
#define FRAG_COLOR_AD0 1
#define FRAG_COLOR_AD1 2
#define FRAG_COLOR_AD2 3

layout(location = FRAG_COLOR) out vec4 COLOR_OUT;

uniform sampler2D textureSampler;
uniform float spacing; // real distance between texture samples 

in vec2 fTexcoord;

void main()
{
	float heightCentre  = textureOffset(textureSampler, fTexcoord, ivec2(0.0, 0.0)).r; 
    float heightLeft    = textureOffset(textureSampler, fTexcoord, ivec2(-1.0, 0.0)).r; 
    float heightRight   = textureOffset(textureSampler, fTexcoord, ivec2(1.0, 0.0)).r; 
    float heightBottom  = textureOffset(textureSampler, fTexcoord, ivec2(0.0, -1.0)).r; 
    float heightTop     = textureOffset(textureSampler, fTexcoord, ivec2(0.0, 1.0)).r; 

    vec3 lr = vec3(spacing, heightRight - heightLeft, 0.0);
    vec3 bt = vec3(0.0, heightTop - heightBottom, spacing);
    vec3 normal = normalize(cross(lr, bt));
	
	COLOR_OUT = vec4(normal, 1.0);

    //if ( normal.x != 0.0 )
    //    COLOR_OUT = vec4(1.0, 1.0, 0.0, 1.0);
}
